﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef DATACONVERTOR_H
#define DATACONVERTOR_H

#include <QSerialPort>
#include <QFile>
#include <tchar.h>
#include "BurnToolCommon/CAdapter.h"
#include "BurnToolCommon/StdAfx.h"
#include "BurnToolCommon/sop_errno.h"
#include "BurnToolCommon/QCArray.h"
#include "BurnToolCommon/Global.h"
#include "BurnToolCommon/StringClass.h"
#include "BurnDlg/BinFileModel.h"

namespace constantValue {
    const U32 NDM_MAC_ADDR_LEN = 6;
    const U32 MAX_METER_ADDR_BYTE = 6;

    const U32 NDM_UTIL_BUF_SIZE_MAX = 2048;
    const U32 VALID_EXPRESSION_OR_HEAD = 3;
    const U32 VALID_EXPRESSION_HEAD = 2;
    const U32 VALID_EXPRESSION = 1;
    const U32 INVALID_EXPRESSION = 0;
    const U32 NDM_METER_ADD_OLD_LEN = 17;
    const U32 NDM_METER_ADD_NEW_LEN = 12;
    const U32 DECIMAL_VALUE = 10;
    const U32 INTERVAL_EACH_FILE_FOR_FWPKG = 16;
    const QString FWPKG_HEAD_NEW_IMAGE_NAME = "new";
}
using namespace constantValue;
using CWStringArray = QCArray<std::wstring, std::wstring &>;
class DataConvertor {
public:
    static bool CheckLoader(BIN_INFO info, int ackFlag);
    static void AnalysMultifBin(QString strPath, BIN_INFO_ARRAY &infoArray, QString &version,
                                bool &isEncrypt, ENCRYPT_BUFFER &buffer);
    static void AnalysMultifBin(QString strPath, BIN_INFO_ARRAY &infoArray, QString &version, ENCRYPT_BUFFER buffer);
    static bool AnalysMultifBinSub(QString strPath, BIN_INFO_ARRAY &infoArray, QString &version,
                                   QFile &binFile, const qint64 &fileLen, bool &isEncrypt,
                                   ENCRYPT_BUFFER &buffer);
    static void AnalysMultifBinSubAdd(const QString &strPath, const qint64 &fileLen, BIN_INFO_ARRAY &infoArray);
    static void ParseNewFwpkg(CHAR *pBurnFile, const U32 &fileLen,
                              QString &version, const QString &strPath, BIN_INFO_ARRAY &infoArray);
    static void ParseNewFwpkgInfo(const QString &strPath, BIN_INFO_ARRAY &infoArray,
        FWPKG_HEAD_NEW *fwpkgHead, bool isShowInUi);
    static void ParseNormalFwpkg(FWPKG_HEAD *fwpkgHead, const U32 &fileLen,
                                 const QString &strPath, BIN_INFO_ARRAY &infoArray);
    static void ParseFwpkgInfo(const QString &strPath, BIN_INFO_ARRAY &infoArray,
        FWPKG_HEAD *fwpkgHead, bool isShowInUi);
    static void DeleteSpace(QString &str);
    static bool IsFigureChar(QChar ch);
    static bool IsDecNumChar(QChar ch);
    static bool IsHexNumChar(QChar ch);
    static U32 Str2U32(QString str, int base);
    static BYTE Char2Figure(QChar ch); // only lower 4 bits

    // 自动识别十进制和十六进制，带0x为十六进制，否则十进制，0开头也为十进制
    static U32 Str2U32DecHex(const QString &str);
    static U32 ByteStr2Buf(const QString &strByte0, PBYTE pucBuf, U32 &pulLen);
    static BYTE DbChar2Figure(QChar cHig, QChar cLow);            // lower 4 bits and higher 4 bits
    static TCHAR Figure2Char(BYTE fig);                           // only lower 4 bits
    static U32 Figure2DbChar(BYTE fig, TCHAR &cHig, TCHAR &cLow); // lower 4 bits and higher 4 bits
    static U32 Buf2ByteStr(const BYTE * const pucBuf, U32 ulLen, QString &strByte);
    static bool IsVisibleAscii(U8 item);
    static bool FindChar(const unsigned char *data, unsigned int dataLen, char ch);
    static QString GetAppPath();
    static QString GetBurnToolConfigPathNew();
    static bool AllPathFileExists(std::vector<QString> &path, std::vector<QString> &result);
    static QStringList GetValidBaud();
    static QStringList GetValidPackageSize();
    static QStringList GetValidDatabit();
    static QStringList GetValidStopbit();
    static QStringList GetValidParity();
    static QStringList GetValidFlowctrl();
    static void RefreshComList(QStringList &result);
    static QString ParityChar2QString(QSerialPort::Parity parity);
    static QSerialPort::Parity ParityQString2Char(QString strParity);
    static int FlowctrlQString2Int(QString strFlowctrl);
    static QString FlowctrlInt2QString(int flowctrl);
    static bool DecryptAESKeyAndWriteFile(const QString &in, BIN_INFO_ARRAY &infoArray,
                                          QString &version, ENCRYPT_BUFFER &buffer);
    static bool DecryptAndWriteFile(BYTE *keyFile, const QString &dataFile, BIN_INFO_ARRAY &infoArray,
                                    QString &version, ENCRYPT_BUFFER &buffer);
    static QStringList GetValidPackageSize3X();
    static QByteArray CodeGB2312(QString &fileName, const QString &coded);
    static QByteArray Bits2Bytes(const QBitArray &bits);
    static bool SaveSpiltFwpkgFile(QMap<int, BIN_INFO> mapBinFileInfo, QString saveFileName, QString importFilePath);
    static bool CreateNewFwpkgFile(QMap<int, BIN_INFO> mapBinFileInfo, QString newFileName, QString importFilePath);
    static void SetFileIndex(QMap<int, BIN_INFO> &actualMapBinFileInfo, bool isNewFwpkgMode);
    static void CopyBinInfo(BIN_INFO inputBinInfo, BIN_INFO &outPutBinInfo);
    static bool AddNotShowUiData(QMap<int, BIN_INFO> mapBinFileInfo, QMap<int, BIN_INFO> &actualMapBinFileInfo,
        bool &isNewFwpkgMode, QString importFilePath);
    static bool WritePartFileToFwpkg(QMap<int, BIN_INFO> actualMapBinFileInfo,
        bool isNewFwpkgMode, QFile &fwpkgNewFile, QDataStream &qDataStream);
    static bool WriteImageHeadToFwpkg(QMap<int, BIN_INFO> actualMapBinFileInfo,
        bool isNewFwpkgMode, QFile &fwpkgNewFile, QDataStream &qDataStream);
    static bool WriteFwpkgHead(QMap<int, BIN_INFO> actualMapBinFileInfo,
        bool isNewFwpkgMode, QFile &fwpkgNewFile, QDataStream &qDataStream, QString saveFileName);
protected:
    explicit DataConvertor(); // protected constructor used by dynamic creation
    virtual ~DataConvertor();
private:
    static MyStringClass::StringClass stringClass;
};

#endif // DATACONVERTOR_H
